package cn.bgs.controller;

import cn.bgs.pojo.Grade;
import cn.bgs.pojo.VoAll.GradeVo;
import cn.bgs.pojo.Student;
import cn.bgs.service.GradeService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author Mr.JIA
 * @DATE 2021/8/2 0002 8:35
 * @PROJECT_NAME mybatis_phoenix
 * @PACKAGE_NAME cn.bgs.controller
 */
@Controller
@RequestMapping("/gra")
public class GradeController {

    Logger log = LogManager.getLogger();

    @Autowired
    private GradeService gradeService;

    //跳转页面
    @RequestMapping("/sel")
    public String sel(){
        return "/view/grade";
    }

    //添加学生
    //跳转以及形成键值对
    @RequestMapping("/findGra/{pkId}")
    public String findGra(@PathVariable Integer pkId, Model mo){
        log.info("gradegradegradegradegradegrade"+pkId);
        Grade grade = gradeService.selectByPrimaryKey(pkId);
        mo.addAttribute("grade",grade);
        return "/view/addGradeStudent";
    }

    //查看
    //跳转并查看班级内学生
    @RequestMapping("/selGraStu/{gradeId}")
    public String selGraStu(@PathVariable Integer gradeId, Model mo){
        log.info("gradegradegradegradegradegrade"+gradeId);
        mo.addAttribute("grade",gradeId);
        return "/view/selGraStu";
    }



    //添加 grade
    @RequestMapping("/addGrade")
    @ResponseBody
    public Integer addGrade(@RequestBody String addGra){
        JSONObject jsonObject = JSONObject.parseObject(addGra);
        Grade gra = new Grade();
        gra.setGradeId(jsonObject.getString("gradeId"));
        gra.setGradeName(jsonObject.getString("gradeName"));
        gra.setClassTeacherId(jsonObject.getString("classTeacherId"));
        gra.setClassTeacherName(jsonObject.getString("classTeacherName"));
        gra.setLecturerId(jsonObject.getString("lecturerId"));
        gra.setLecturerName(jsonObject.getString("lecturerName"));
        gra.setStudentsNumber(jsonObject.getInteger("studentsNumber"));
        gra.setClassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        gra.setClassroomClassroomNumber(jsonObject.getString("classroomClassroomNumber"));
        gra.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();
        gra.setCreateTime(now.getTime());
        gra.setUpdateTime(now.getTime());
        
        return gradeService.addGrade(gra);
    }

    //删除 grade
    @RequestMapping("/delGrade")
    @ResponseBody
    public Integer delGrade(@RequestBody String del){
        JSONObject jsonObject = JSONObject.parseObject(del);
        Integer pkId = jsonObject.getInteger("pkId");
        log.info("Grade pkid===========:::::::::::::"+pkId);
        return gradeService.delGrade(pkId);

    }

    //修改 grade
    @RequestMapping("/updGrade")
    @ResponseBody
    public Integer updGrade(@RequestBody String upd){
        JSONObject jsonObject = JSONObject.parseObject(upd);
        Grade gra = new Grade();
        gra.setPkId(jsonObject.getInteger("pkId"));
        gra.setGradeId(jsonObject.getString("gradeId"));
        gra.setGradeName(jsonObject.getString("gradeName"));
        gra.setClassTeacherId(jsonObject.getString("classTeacherId"));
        gra.setClassTeacherName(jsonObject.getString("classTeacherName"));
        gra.setLecturerId(jsonObject.getString("lecturerId"));
        gra.setLecturerName(jsonObject.getString("lecturerName"));
        gra.setStudentsNumber(jsonObject.getInteger("studentsNumber"));
        gra.setClassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        gra.setClassroomClassroomNumber(jsonObject.getString("classroomClassroomNumber"));

        Calendar instance = Calendar.getInstance();
        gra.setUpdateTime(instance.getTime());

        return gradeService.updGrade(gra);
    }

    //查询  grade
    @ResponseBody
    @RequestMapping("/findAllGrade")
    public Map<String,Object> findAllGrade(@RequestParam(required=false,defaultValue="1") int page,
                                         @RequestParam(required=false,defaultValue="7") int limit,
                                         GradeVo vo){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);

        log.info("String gradeName, String classTeacherName, String lecturerName, Integer studentsNumber：：：：：：："
                +vo.getGradeName()+"   "
                +vo.getClassTeacherName()+"   "
                +vo.getLecturerName()+"   "
                +vo.getStudentsNumber()+"   "

        );
        List<Student> classRoom = gradeService.findAllGrade(vo);
        log.info("data======::::::::"+classRoom);
        // 使用pageInfo包装查询
        PageInfo<Student> rolePageInfo = new PageInfo<>(classRoom);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }



}
